# Function for model tables
mytable <- function(model, livingcond){
  
  sum <- summary(model)$fixed %>%
    as.data.frame %>%
    tibble::rownames_to_column("Covariate") %>%
    dplyr::select(-Bulk_ESS, -Tail_ESS, -Est.Error)%>%
    dplyr::rename(lower = "l-95% CI") %>%
    dplyr::rename(upper = "u-95% CI") %>%
    mutate(upper = round(upper, digits = 2), lower = round(lower, digits = 2)) %>%
    mutate(CI = (paste0("[", lower, "," , upper,"]")))%>%
    mutate(Covariate = as.character(Covariate)) 
  
  sum$NEFF <- effective_sample(model)[,2]
  sum$`CI excludes 0`<- ifelse(sum$lower > 0 | sum$upper < 0, "yes", "no")
  
  Rhat <- round(max(sum$Rhat), digits=2)
  obs <- as.numeric(nrow(model$data))
  note <- paste0("Observations: ", obs,",", " Max. $\\hat{R}$: ", Rhat)
  
  table <- sum %>%
    filter(!grepl("as.factor",Covariate))
  
  if (nrow(table) > 12) {
    if ("light30_diff" %in% table$Covariate) {
      target <- c("Education","Diff. to nat. development","Education * Diff. to nat. development",
                  "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                  "Services (EA)","Infrastructure (EA)","Conflict (EA)")
    } 
    if ("change3_30_log" %in% table$Covariate)  {
      target <- c("Education","Local Growth","Education * Local Growth",
                  "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                  "Services (EA)","Infrastructure (EA)","Conflict (EA)")
      
    }
    if ("light_dyn_log" %in% table$Covariate) {
      target <- c("Education","Local Development","Education * Local Development",
                  "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                  "Services (EA)","Infrastructure (EA)","Conflict (EA)")
      
    } 
    if ("light_10_log" %in% table$Covariate) {
      target <- c("Education","Local Development","Education * Local Development",
                  "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                  "Services (EA)","Infrastructure (EA)","Conflict (EA)")
      
    } 
    if ("light_50_log" %in% table$Covariate) {
      target <- c("Education","Local Development","Education * Local Development",
                  "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                  "Services (EA)","Infrastructure (EA)","Conflict (EA)")
      
    } 
    if ("light_30_log" %in% table$Covariate) {
      target <- c("Education","Local Development","Education * Local Development",
                  "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                  "Services (EA)","Infrastructure (EA)","Conflict (EA)")
    } 
  } else {
    target <- c("Education","Local Development",
                "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                "Services (EA)","Infrastructure (EA)","Conflict (EA)")
  } 
  
  
  if(livingcond == T){
    if(nrow(table) > 15) {
      if ("light30_diff" %in% table$Covariate) {
        target <- c("Education","Diff. to nat. development","Education * Diff. to nat. development",
                    "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                    "Services (EA)","Infrastructure (EA)","Conflict (EA)", "tau1","tau2","tau3" ,"tau4")
      } 
      if ("change3_30_log" %in% table$Covariate)  {
        target <- c("Education","Local Growth","Education * Local Growth",
                    "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                    "Services (EA)","Infrastructure (EA)","Conflict (EA)", "tau1","tau2","tau3" ,"tau4")
        
      }
      if ("light_dyn_log" %in% table$Covariate) {
        target <- c("Education","Local Development","Education * Local Development",
                    "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                    "Services (EA)","Infrastructure (EA)","Conflict (EA)", "tau1","tau2","tau3" ,"tau4")
        
      } 
      if ("light_10_log" %in% table$Covariate) {
        target <- c("Education","Local Development","Education * Local Development",
                    "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                    "Services (EA)","Infrastructure (EA)","Conflict (EA)", "tau1","tau2","tau3" ,"tau4")
        
      } 
      if ("light_50_log" %in% table$Covariate) {
        target <- c("Education","Local Development","Education * Local Development",
                    "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                    "Services (EA)","Infrastructure (EA)","Conflict (EA)", "tau1","tau2","tau3" ,"tau4")
      } 
      
      if ("light_30_log" %in% table$Covariate) {
        target <- c("Education","Local Development","Education * Local Development",
                    "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                    "Services (EA)","Infrastructure (EA)","Conflict (EA)", "tau1","tau2","tau3" ,"tau4")
      } }
    else {
      target <- c("Education","Local Development",
                  "Age","Female","Urban","Unemployment","Ethnic Grievances","Media Consumption",
                  "Services (EA)","Infrastructure (EA)","Conflict (EA)", "tau1","tau2","tau3" ,"tau4") } 
  } 

    
  table$Covariate[table$Covariate == "educ"] <- "Education"
  table$Covariate[table$Covariate == "age"] <- "Age"
  table$Covariate[table$Covariate == "female"] <- "Female"
  table$Covariate[table$Covariate == "urban"] <- "Urban"
  table$Covariate[table$Covariate == "unempl"] <- "Unemployment"
  table$Covariate[table$Covariate == "ethfair"] <- "Ethnic Grievances"
  table$Covariate[table$Covariate == "media"] <- "Media Consumption"
  table$Covariate[table$Covariate == "ea_service"] <- "Services (EA)"
  table$Covariate[table$Covariate == "ea_infrastructure"] <- "Infrastructure (EA)"
  table$Covariate[table$Covariate == "conflict"] <- "Conflict (EA)"
  
  table$Covariate[table$Covariate == "light30_diff"] <- "Diff. to nat. development"
  table$Covariate[table$Covariate == "educ:light30_diff"] <- "Education * Diff. to nat. development"
  table$Covariate[table$Covariate == "change3_30_log"] <- "Local Growth"
  table$Covariate[table$Covariate == "educ:change3_30_log"] <- "Education * Local Growth"
  table$Covariate[table$Covariate == "light_dyn_log"] <- "Local Development"
  table$Covariate[table$Covariate == "educ:light_dyn_log"] <- "Education * Local Development"
  table$Covariate[table$Covariate == "light_10_log"] <- "Local Development"
  table$Covariate[table$Covariate == "educ:light_10_log"] <- "Education * Local Development"
  table$Covariate[table$Covariate == "light_50_log"] <- "Local Development"
  table$Covariate[table$Covariate == "educ:light_50_log"] <- "Education * Local Development"
  table$Covariate[table$Covariate == "light_30_log"] <- "Local Development"
  table$Covariate[table$Covariate == "educ:light_30_log"] <- "Education * Local Development"
  
  table$Covariate[table$Covariate == "Intercept[1]"] <- "tau1"
  table$Covariate[table$Covariate == "Intercept[2]"] <- "tau2"
  table$Covariate[table$Covariate == "Intercept[3]"] <- "tau3"
  table$Covariate[table$Covariate == "Intercept[4]"] <- "tau4"
  
  table <- table[match(target, table$Covariate),]
  table <- table %>%
    dplyr::select(Covariate, Estimate, CI, `CI excludes 0`, NEFF)
  
  table$`CI excludes 0`[table$Covariate == "tau1"] <- ""
  table$`CI excludes 0`[table$Covariate == "tau2"] <- ""
  table$`CI excludes 0`[table$Covariate == "tau3"] <- ""
  table$`CI excludes 0`[table$Covariate == "tau4"] <- ""
  
  
  stargazer(table, 
            summary=F, type="text", rownames = F, digits = 2,
            covariate.labels=c("","Posterior Mean", "95% CI","CI excludes 0", "NEFF"), 
            notes = note, notes.align = "r")
  
}